package com.upcode.weighing.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Feign配置
 * 使用FeignClient进行服务间调用，传递headers信息
 */
@Configuration
public class FeignConfig implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        if(null == attributes){
            return;
        }
        if(null == attributes.getRequest()){
            return;
        }
        HttpServletRequest request = attributes.getRequest();
        if(StringUtils.isBlank(request.getHeader("Authorization"))){
            return;
        }
        //添加token
        requestTemplate.header("Authorization", request.getHeader("Authorization"));

    }
}
